import {
    Body, Controller,
    Get,
    HttpCode, Post, Req,
    UseFilters,
    UseGuards,
    UseInterceptors,
    UsePipes,
    ValidationPipe
} from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
import {
    Crud,
    CrudController,
    CrudRequest,
    Override,

    ParsedRequest
} from '@nestjsx/crud';
import { Request } from 'express';
import { AuthService } from 'libs/auth';
import { HandleStatus } from 'libs/db/entity/enum/handle-status.enum';
import { User } from 'libs/db/entity/user.entity';
import { LoggerInfo, moduleLoggerInfo } from '../_decorator/logger-info';
import { HttpExceptionFilter } from '../_filter/http-exception.filter';
import { LoggingInterceptor } from '../_interceptor/logging.interceptor';
import { LoginUser } from './dto/login-user.dto';
import { UserCrudOps } from './user-crud-ops';
import { UserService } from './user.service';

@Crud(UserCrudOps)
@UseFilters(HttpExceptionFilter)
@UsePipes(ValidationPipe)
@UseInterceptors(LoggingInterceptor)
@ApiTags('管理员管理') //标签
@Controller('user')
export class UserController implements CrudController<User> {
  constructor(
    public service: UserService,
    private readonly authService: AuthService,
  ) {}

  get base(): CrudController<User> {
    return this;
  }

  // // 得到 全部用户
  @Override()
  @UseGuards(AuthGuard('jwt-admin'))
  @ApiBearerAuth()
  @LoggerInfo(new moduleLoggerInfo ('查询管理员用户', HandleStatus.LOG))
  getMany(@ParsedRequest() crudRequest: CrudRequest, @Req() req) {
    return this.base.getManyBase(crudRequest);
  }

  // // 得到一个 用户
  // @Override('getOneBase')
  // getOneAndDoStuff(@ParsedRequest() req: CrudRequest) {
  //   return this.base.getOneBase(req);
  // }

  @UseGuards(AuthGuard('local-admin'))
  @HttpCode(200)
  @LoggerInfo(new moduleLoggerInfo ('用户登录查询', HandleStatus.LOG))
  @Post('login')
  login(@Body() user: LoginUser, @Req() req : Request) {
    return this.authService.login(req.user as User)
  }

  //创建 用户
  // @Override()
  // // @UseGuards(AuthGuard('local'))
  // createOne(
  //   @ParsedRequest() crudRequest: CrudRequest | any,
  //   @ParsedBody() dto: User,
  //   @Req() req,
  // ) {
  //   return this.authService.login(req.user);
  // }

  //查询 个人信息
  @UseGuards(AuthGuard('jwt-admin'))
  @ApiBearerAuth()
  @LoggerInfo(new moduleLoggerInfo ('个人信息查询', HandleStatus.LOG))
  @Get('info')
  getMyInfo(@Req() req) {
    return req.user;
  }

  // //创建 多个 用户
  // @Override()
  // @UseGuards(AuthGuard('jwt-admin'))
  // createMany(
  //   @ParsedRequest() crudRequest: CrudRequest,
  //   @ParsedBody() dto: CreateManyDto<User>,
  //   @Req() req
  // ) {
  //   return this.base.createManyBase(req, dto);
  // }

  // //更新用户
  // @Override('updateOneBase')
  // coolFunction(@ParsedRequest() req: CrudRequest, @ParsedBody() dto: User) {
  //   return this.base.updateOneBase(req, dto);
  // }

  // //替换 用户
  // @Override('replaceOneBase')
  // awesomePUT(@ParsedRequest() req: CrudRequest, @ParsedBody() dto: User) {
  //   return this.base.replaceOneBase(req, dto);
  // }

  // //删除 用户
  // @Override()
  // async deleteOne(@ParsedRequest() req: CrudRequest) {
  //   return this.base.deleteOneBase(req);
  // }
}
